package gui.search;

import gui.main;
import gui.form.OwnerPanel;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import Model.Owner;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;

import dao.DaoOwner;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OwnerSearchPane extends JPanel {
	
	private static final long serialVersionUID = 1L;
	
	private DefaultTableModel model;
	public static OwnerSearchPane instance;
	private JTable table;
	private JPanel resultPane;
	private JScrollPane scrollPane;
	Owner[] owners;
	
	DaoOwner daoOwner = new DaoOwner();
    
	/**
	 * Create the panel.
	 */
	public OwnerSearchPane() {
		model = new DefaultTableModel();
		
		model.addColumn("Name");
		model.addColumn("Last Name");
		model.addColumn("SSN");
		model.addColumn("BirtDate");
		model.addColumn("BirtPlace");
		model.setColumnIdentifiers(new String[] {"Name", "Last Name","SSN","BirtDate","BirtPlace"});
		JButton btnUpdateOwner = new JButton("Update");
		
		JButton btnShowOwner = new JButton("Show");
		btnShowOwner.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			
				addPanel(OwnerShowPane.getInstance());
			
				
			}
		});
		
		JButton btnDeleteOwner = new JButton("Delete");
		
		resultPane = new JPanel();
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addComponent(btnShowOwner, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnUpdateOwner)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(btnDeleteOwner, GroupLayout.PREFERRED_SIZE, 67, GroupLayout.PREFERRED_SIZE)
							.addGap(32))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(resultPane, GroupLayout.DEFAULT_SIZE, 516, Short.MAX_VALUE)
							.addContainerGap())))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(resultPane, GroupLayout.PREFERRED_SIZE, 269, GroupLayout.PREFERRED_SIZE)
					.addGap(11)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnDeleteOwner)
						.addComponent(btnUpdateOwner)
						.addComponent(btnShowOwner))
					.addContainerGap(103, Short.MAX_VALUE))
		);
		
		scrollPane = new JScrollPane();
		GroupLayout gl_resultPane = new GroupLayout(resultPane);
		gl_resultPane.setHorizontalGroup(
			gl_resultPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_resultPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_resultPane.setVerticalGroup(
			gl_resultPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_resultPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 238, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(20, Short.MAX_VALUE))
		);
		
		table = new JTable(model);
		table.setSurrendersFocusOnKeystroke(true);
		
		table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
		table.setRowSelectionAllowed(true);
		table.setCellSelectionEnabled(false);

		table.setColumnSelectionAllowed(false);
	     
		table.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				int i = e.getFirstIndex();
				
				Owner owner = owners[i];
				
				owner.setName(String.valueOf(model.getValueAt(i, 0)));
				owner.setName(String.valueOf(model.getValueAt(i, 1)));
				owner.setName(String.valueOf(model.getValueAt(i, 2)));
				
				OwnerPanel ownerPanel = new OwnerPanel(owner);
				main.getMain().addOwnerPane(ownerPanel);
				
			}
		});
		
		scrollPane.setColumnHeaderView(table);
		resultPane.setLayout(gl_resultPane);
		setLayout(groupLayout);
		
	}
	
	public void fillTable(List<Owner> own){

		int rows = model.getRowCount();
		for(int i=0; i < model.getRowCount(); i++)
			model.removeRow(0);
		
		owners = new Owner[rows];
		owners = own.toArray(owners);
		
		for ( Owner p : own)
		{			
		    model.addRow(new String[] {p.getName(),p.getLastname(),p.getSSN(),p.getBirthdate().toString(),p.getBirthplace().toString()});
		}
		
	} 
	public void addTable(){
		resultPane.removeAll();
		resultPane.setLayout(new BorderLayout());                        
		resultPane.add(scrollPane, BorderLayout.CENTER);
				
	}
	
    void addPanel(JPanel p){    	
		resultPane.removeAll();
		resultPane.setLayout(new BorderLayout());                        
		resultPane.add(p, BorderLayout.CENTER);
		resultPane.revalidate();
		resultPane.repaint();
    }

    


}
